package cn.sdormitory.smartdor.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.sdormitory.common.constant.CommonConstant;
import cn.sdormitory.common.utils.DateTimeUtils;
import cn.sdormitory.smartdor.dao.SdRepairDao;
import cn.sdormitory.smartdor.entity.SdRepair;
import cn.sdormitory.smartdor.service.RepairDeviceService;
import cn.sdormitory.smartdor.service.SdRepairService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;

/**
 * @创建人：zhouyang
 * @创建时间：2021/10/13 15:04
 * @version：V1.0
 */
@Slf4j
@Service("sdRepairService")
public class SdRepairServiceImpl extends ServiceImpl<SdRepairDao, SdRepair> implements SdRepairService {
    @Resource
    private RepairDeviceService repairDeviceService;
    @Override
    public IPage<SdRepair> getPage(Map<String, Object> params) {
        int pageSize = Integer.parseInt(String.valueOf(params.get("pageSize")));
        int pageNum = Integer.parseInt(String.valueOf(params.get("pageNum")));

//        String devicesValue = (String) params.get("devicesValue");
        String beginTime = (String) params.get("beginTime");
        String endTime = (String) params.get("endTime");
        String buildingNo = (String) params.get("buildingNo");
        String storey = (String) params.get("storey");
        String dormitoryNo = (String) params.get("dormitoryNo");
        String status = (String) params.get("status");

        LambdaQueryWrapper<SdRepair> wrapper = new LambdaQueryWrapper<>();
//        if (StrUtil.isNotEmpty(devicesValue)) {
//            wrapper.eq(SdRepair::getDevicesValue, devicesValue);
//        }
        if (StrUtil.isNotEmpty(beginTime)) {
            wrapper.gt(SdRepair::getCreateTime, beginTime);
        }else{
            wrapper.gt(SdRepair::getCreateTime, DateTimeUtils.getMonthFirstDay());
        }
        if (StrUtil.isNotEmpty(endTime)) {
            wrapper.lt(SdRepair::getCreateTime, endTime);
        }else{
            wrapper.lt(SdRepair::getCreateTime, DateTimeUtils.getMonthLastDay());
        }
        if (StrUtil.isNotEmpty(buildingNo)) {
            wrapper.eq(SdRepair::getBuildingNo, buildingNo);
        }
        if (StrUtil.isNotEmpty(storey)) {
            wrapper.eq(SdRepair::getStorey, storey);
        }
        if (StrUtil.isNotEmpty(dormitoryNo)) {
            wrapper.eq(SdRepair::getDormitoryNo, dormitoryNo);
        }
        if (StrUtil.isNotEmpty(status)) {
            wrapper.eq(SdRepair::getStatus, status);
        }

        wrapper.apply(params.get(CommonConstant.SQL_FILTER) != null, (String) params.get(CommonConstant.SQL_FILTER));
        return page(new Page<>(pageNum, pageSize), wrapper);
    }

    @Override
    public SdRepair getSdRepairById(Long id) {
        return getById(id);
    }

    @Override
    public int create(SdRepair repair) {
        int result = this.baseMapper.insert(repair);
        //保存报修与设备关系
        repairDeviceService.insertRepairAndRepairDevice(repair.getId(), repair.getDeviceIds());
        return result;
    }

    @Override
    public int delete(Long id) {
        return this.baseMapper.deleteById(id);
    }

    @Override
    public int update(Long id, SdRepair repair) {
        repair.setId(id);
        //先删除报修与设备关系
        repairDeviceService.delAndCreateDevice(repair.getId(), repair.getDeviceIds());
        repair.setModifyTime(new Date());
        return this.baseMapper.updateById(repair);
    }

    @Override
    public int deleteByIds(Long[] ids) {
        return this.baseMapper.deleteBatchIds(Arrays.asList(ids));
    }

    @Override
    public int updateStatus(Long id, String status) {
        SdRepair repair = new SdRepair();
        repair.setId(id);
        repair.setStatus(status);
        repair.setModifyTime(new Date());
        return this.baseMapper.updateById(repair);
    }

    @Override
    public SdRepair getRepairInfoById(Long id) {
        return this.baseMapper.getRepairInfoById(id);
    }
}
